package com.lcxw.hxzbapi.tencentim.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.tencentim.FeignService.ImManager;
import com.tencentyun.TLSSigAPIv2;
import java.util.Random;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import springfox.documentation.service.ApiListing;

@Component
@Slf4j
public class TencentImRealization {

    @Resource
    ImManager imManager;

    @Resource
    private TencentImConfig configBean;



    /**
     * 1、导入单个用户
     */
    public void importImUser(long userId) {
        this.importImUser(userId + "");
    }

    public void importImUser(String userId) {

        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(configBean.getAppid(), configBean.getKey());
        long random = new Random().nextLong();
        String admin = configBean.getAdmin();
        String key = tlsSigAPIv2.genSig(admin, configBean.getExpire());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Identifier", userId);
        String result = imManager
            .importAccount(configBean.getAppid(), admin, key, random, jsonObject);
        log.info("import user {} to tencent im " + result, userId);
    }

    /**
     * 2、修改用户的昵称/头像等
     */
    public void updateImUserInfo(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(configBean.getAppid(), configBean.getKey());
        long random = new Random().nextLong();
        String admin = configBean.getAdmin();
        String key = tlsSigAPIv2.genSig(admin, configBean.getExpire());
        String result = imManager.updateUserInfo(configBean.getAppid(), admin, key, random, imBody);
        log.info("update im user info " + result);
    }

    /*
     * 3、后台系统推送普通群消息
     */
    public void sendGroupMsg(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(configBean.getAppid(), configBean.getKey());
        long random = new Random().nextLong();
        String admin = configBean.getAdmin();
        String key = tlsSigAPIv2.genSig(admin, configBean.getExpire());
        String result = imManager.sendGroupMsg(configBean.getAppid(), admin, key, random, imBody);
        log.info("发送群消息结果：" + result);
    }

    /**
     * 4、解散群组
     */
    public void destroyGroup(JSONObject imBody) {
        TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(configBean.getAppid(), configBean.getKey());
        long random = new Random().nextLong();
        String admin = configBean.getAdmin();
        String key = tlsSigAPIv2.genSig(admin, configBean.getExpire());
        String result = imManager.destroyGroup(configBean.getAppid(), admin, key, random, imBody);
        log.info("解散群组结果：" + result);
    }

    /**
     * 构造imBody消息的方法
     */
    public JSONObject generateImBody(String msg, String roomId) {
        JSONObject imBody = new JSONObject();
        JSONArray msgBody = new JSONArray();
        imBody.put("GroupId", roomId);
        imBody.put("Random", new Random().nextInt(10000000));
        JSONObject msgCustomElem = new JSONObject();
        msgCustomElem.put("MsgType", "TIMCustomElem");

        JSONObject msgContentObj = new JSONObject();
        msgContentObj.put("Data", "system");
        msgContentObj.put("Ext", msg);
        msgCustomElem.put("MsgContent", msgContentObj);

        msgBody.add(msgCustomElem);
        imBody.put("MsgBody", msgBody);

        return imBody;

    }


}
